package com.snail.app.push;

import com.snail.common.ResultUtil;
import com.snail.common.entity.BaseController;
import com.snail.common.util.StringUtil;
import com.snail.app.gather.service.PushMqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @description
 * @date 2021/12/9
 * @autor houyanfeng
 */
@Api
@Slf4j
@RestController
@RequestMapping("push/mq")
public class PushMqController extends BaseController {
    @Autowired
    private PushMqService pushMqService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /** 通用数据抓取并上传平台方法 */
    @ApiOperation(value="单类型数据同步", notes="")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "triggerId", value = "触发条件记录ID", dataType = "string", paramType = "query", example = "hd31_warehouse")
        ,@ApiImplicitParam(name = "startTime", value = "开始时间（yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query", required = false, example = "hd31_warehouse")
        ,@ApiImplicitParam(name = "endTime", value = "结束时间（yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query", required = false, example = "hd31_warehouse")
        ,@ApiImplicitParam(name = "batch", value = "批量数量", dataType = "int", paramType = "query", required = false, example = "hd31_warehouse")
    })
    @GetMapping("/one")
    public Map<String,Object> pushMqOne(@RequestParam String triggerId){
        Map<String,Object> params = new HashMap<>();
        if(!StringUtil.isEmpty(request.getParameter("startTime"))){
            params.put("startTime", request.getParameter("startTime"));
        }
        if(!StringUtil.isEmpty(request.getParameter("endTime"))){
            params.put("endTime", request.getParameter("endTime"));
        }
        if(!StringUtil.isEmpty(request.getParameter("batch"))){
            params.put("batch", request.getParameter("batch"));
        }
        try {
            pushMqService.pushMq(params,triggerId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(400, e.getMessage(), null);
        }
        return ResultUtil.result();
    }

    @GetMapping("/test")
    public Map<String,Object> test(){
        return ResultUtil.result();
    }
}
